import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.text.Font;

//@author A0096724W
public class Main extends Application {

    	/**********************
    	 * Private Variables
    	 **********************/
	AnchorPane root;
	
	/*
	 * Used for calculating distance moved when dragging GUI
	 */
	private double xOffset = 0;
	private double yOffset = 0;
	
	@Override
	public void start(final Stage primaryStage) {
	    try {

		//Load resources needed, i.e. FXML file and font
		AnchorPane root = (AnchorPane) FXMLLoader.load(getClass().getResource("/Sample.fxml"));
		Font.loadFont(getClass().getResourceAsStream("fontawesome-webfont.ttf"), 20);
		primaryStage.initStyle(StageStyle.TRANSPARENT);

		Label closeButton = initCloseButton();
		Label minimizeButton = initMinimizeButton(primaryStage);
		
		//Add items to GUI
		root.getChildren().add(closeButton);
		root.getChildren().add(minimizeButton);

		setGuiMovable(primaryStage, root);
		
		//Load CSS file used to style GUI
		Scene scene = new Scene(root);
		scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
		primaryStage.setScene(scene);
		primaryStage.show();
	    } catch(Exception e) {
		e.printStackTrace();
	    }
	}

	private void setGuiMovable(final Stage primaryStage, AnchorPane root) {
	    root.setOnMousePressed(new EventHandler<MouseEvent>() {
		@Override
		public void handle(MouseEvent event) {
		    xOffset = event.getSceneX();
		    yOffset = event.getSceneY();
		}
	    });

	    root.setOnMouseDragged(new EventHandler<MouseEvent>() {
		@Override
		public void handle(MouseEvent event) {
		    primaryStage.setX(event.getScreenX() - xOffset);
		    primaryStage.setY(event.getScreenY() - yOffset);
		}
	    });
	}

	private Label initMinimizeButton(final Stage primaryStage) {
	    Label minimizeButton = new Label();
	    minimizeButton.setText("\uf068");
	    minimizeButton.setFont(Font.font(null, 15));
	    minimizeButton.getStyleClass().add("fontawesome");
	    minimizeButton.setLayoutX(947);
	    minimizeButton.setLayoutY(10);
	    
	    minimizeButton.setOnMouseClicked(new EventHandler<MouseEvent>() {
	        public void handle(MouseEvent me) {
	            primaryStage.setIconified(true);
	        }
	    });
	    return minimizeButton;
	}

	private Label initCloseButton() {
	    Label closeButton = new Label();
	    closeButton.setText("\uf00d");
	    closeButton.setFont(Font.font("FontAwesome", 18));
	    closeButton.getStyleClass().add("fontawesome");
	    closeButton.setLayoutX(975);
	    closeButton.setLayoutY(6);
	    
	    closeButton.setOnMouseClicked(new EventHandler<MouseEvent>() {
	        public void handle(MouseEvent me) {
	            System.exit(0);
	        }
	    });
	    return closeButton;
	}

	public static void main(String[] args) {
		launch(args);
	}
}
